package com.pukea.app.medical.zyyb;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.renyibao.mteaf.ext.client.util.ExtTools;
import com.renyibao.mteaf.ext.client.weixin.msg.WxPassiveMessage;

import net.minidev.json.JSONObject;

public class YbResponse {
	public static String ERR_ZY_YBMZXX = "501";
	public static String ERR_UPDATE_PAT = "502";
	public static String ERR_ZYYBID = "503";
		
	public boolean isSuccess = false;
	
	public String errCode = "";
	
	public String errMessage = "";
	
	public Object extReturnObject;

	public boolean isSuccess() {
		return isSuccess;
	}

	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

	public String getErrCode() {
		return errCode;
	}

	public void setErrCode(String errCode) {
		this.errCode = errCode;
	}

	public String getErrMessage() {
		return errMessage;
	}

	public void setErrMessage(String errMessage) {
		this.errMessage = errMessage;
	}

	public Object getExtReturnObject() {
		return extReturnObject;
	}

	public void setExtReturnObject(Object extReturnObject) {
		this.extReturnObject = extReturnObject;
	}
	
	public String toJson(){
		StringBuffer strJson = new StringBuffer("{\"errCode\":\"");
		strJson.append(isSuccess?"200":this.getErrCode()).append("\",\"errMessage\":\"");
		strJson.append(this.getErrMessage()).append("\"");
		
		if(extReturnObject == null){
			strJson.append("}");
		}
		else{
			
			if(extReturnObject instanceof JSONObject){
				
				strJson.append(",\"extReturn\":");

				JSONObject jo = (JSONObject)extReturnObject;

				strJson.append(jo.toJSONString());


			}
			else if(extReturnObject instanceof Map){
				strJson.append(",\"extReturn\":{");

				Map smsMap = (HashMap)extReturnObject;
				Iterator it = smsMap.entrySet().iterator();
				
				int i = 0;
				while (it.hasNext()){
					Entry entry = (java.util.Map.Entry) it.next();
					
					Object ob = (Object)entry.getValue();
					
					String value = "";
					if(ob instanceof String){
						value = ExtTools.nvl((String)ob);
					}
					else if(ob instanceof Integer){
						value = ""+(Integer)ob;
					}
					
					if(i>0){
						strJson.append(",");
					}
					strJson.append("\"").append(entry.getKey()).append("\":\"").append(value).append("\"");
					i++;
				}
				
				strJson.append("}");
			}
			
			else if(extReturnObject instanceof WxPassiveMessage){
				
				WxPassiveMessage wxpm = (WxPassiveMessage)extReturnObject;
				
				strJson.append(",\"extReturn\":\"");
				strJson.append(wxpm.toXml());
				strJson.append("\"}");
				
			}
			strJson.append("}");
			
		}
		
		return strJson.toString();
	}
}
